package com.ossean.match.matchprocess;

import java.io.IOException;
import java.sql.SQLException;
import java.text.DecimalFormat;
import java.util.List;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import com.ossean.match.dao.ProjectDao;
import com.ossean.match.lucene.LuceneIndex;
import com.ossean.match.model.Project;

@Component("prjtomemomatch")
public class PrjToMemoMatch implements Runnable {
	private Logger logger = LoggerFactory.getLogger(getClass());
	private int step = 10000; // setting the numbers of projects for every reading
	private MatchIncrement matchIncrement = null;
	@Resource
	private ProjectDao projectDao;
	@Qualifier("match")
	@Autowired
	private Match match;

	public void setMatchIncrement(MatchIncrement matchIncrement) {
		this.matchIncrement = matchIncrement;
	}

	public void prjToMemoIncrement() throws IOException, SQLException {
		matchIncrement.setCurrentMatchDone(false);
		long start = System.currentTimeMillis();
		matchIncrement.getMatchRecord();
		matchIncrement.setCurrentMemo(matchIncrement.getMemoHistory());
		int startId = matchIncrement.getPrjHistory();
		int endId = startId + step;
		int lastPrjId = matchIncrement.getLastPrjId();
		if ((lastPrjId - endId) <= step) {
			endId = lastPrjId;
		}
		while (startId < lastPrjId) {
			long batchStart = System.currentTimeMillis();
			List<Project> newPrjs = projectDao.getBatchPrjs(startId, endId);// getting projects' id and prjName in batches
			long end1 = System.currentTimeMillis();
			logger.info("批量读项目: " + (end1 - batchStart) + "ms");
			if (newPrjs != null) {
				match.prjToMemoMatch(newPrjs, matchIncrement);
				matchIncrement.setCurrentProject(endId);
				if (startId > matchIncrement.getPrjIndexed()) {
					// adding projects index and save the record
					LuceneIndex luceneIndex = new LuceneIndex();
					int prjIndexed = luceneIndex.prjIndexIncrement(newPrjs);
					matchIncrement.setPrjIndexed(prjIndexed);
				}
				
			}
			long batchEnd = System.currentTimeMillis();
			DecimalFormat df = new DecimalFormat( "0.00"); 
			double totalTime = (double)(batchEnd - batchStart)/60000;
			double rate = (double)(endId - startId)/((batchEnd - batchStart)/1000);
			logger.info("current Batch prj-->memo total time: " + df.format(totalTime) + "min; "
					     + " average： " + df.format(rate) + " 条/s");
			logger.info("osps_to_relative_memos matched to : "
					+ matchIncrement.getCurrentProject());
			logger.info("osps_to_relative_memos remain_to_match : "
					+ (lastPrjId - endId));
			// finishing matching, writing the record to "record.txt"
			matchIncrement.writeIntoRecord();
			if ((lastPrjId - endId) <= step) {
				startId = endId;
				endId = lastPrjId;
			}
			else {
				startId = endId;
				endId += step;
			}
		}
		long end = System.currentTimeMillis();
		DecimalFormat df = new DecimalFormat( "0.00"); 
		double totalTime = (double)(end - start)/60000;
		logger.info("prj-->memo total time: " + df.format(totalTime) + "min");
		matchIncrement.setCurrentMatchDone(true);
	}
	
	public void run() {
		if (matchIncrement.getTaskToPrjId() != 0) {
			int startId = matchIncrement.getPrjHistory();
			int endId = matchIncrement.getTaskToPrjId();
			long batchStart = System.currentTimeMillis();
			List<Project> newPrjs = projectDao.getBatchPrjs(startId, endId);// getting projects' id and prjName in batches
			long end1 = System.currentTimeMillis();
			logger.info("批量读项目: " + (end1 - batchStart) + "ms");
			if (newPrjs != null) {
				match.prjToMemoMatch(newPrjs, matchIncrement);
			}
			long batchEnd = System.currentTimeMillis();
			DecimalFormat df = new DecimalFormat( "0.00"); 
			double totalTime = (double)(batchEnd - batchStart)/60000;
			double rate = (double)(endId - startId)/((batchEnd - batchStart)/1000);
			logger.info("current Batch prj-->memo total time: " + df.format(totalTime) + "min; "
					     + " average： " + df.format(rate) + " 条/s");
			logger.info("osps_to_relative_memos matched to : "
					+ matchIncrement.getCurrentProject());
			System.exit(0);
		}
		else {
			if (matchIncrement.isNewPrjComming()) {
				logger.info("start osps_to_relative_memos match!!!");
				try {
					prjToMemoIncrement();
				} catch (IOException e) {
					logger.error("IOException: " + e);
				} catch (SQLException e) {
					logger.error("SQLException: " + e);
				}
			}
		}

	}

}
